﻿using Pathfinding;
using Pathfinding.RVO;
using UnityEngine;
using Yoozoo.Gameplay.RTS;
using Yoozoo.Gta.Common;

namespace Yoozoo.Gameplay.City.AI
{
    public class CityOutRoomNPCSeeker : MonoBehaviour
    {
        public Animator animator;
        public ModelInstaning model;
        public Seeker seeker;
        public CityAmbientNPCAIPath aiPath;
        public RVOController rvoController;

        private string eventName;
        private int needTime;
        private float rotateY;

        private int outPointIndex;//外围小人的事件点序号

        private int buildingId;
        private int bIndex;
        //private int entityIndex;

        private RoomData roomData;
        // private NPCPoint oldNpcPoint;
        // private NPCPoint newNpcPoint;

        private bool isShowEvent;

        private float randomWalkTime;
        private bool randomIsWalking;

        private int areaId;

        private void Awake()
        {
            seeker = transform.GetComponent<Seeker>();
            aiPath = transform.GetComponent<CityAmbientNPCAIPath>();
            rvoController = transform.GetComponent<RVOController>();
        }
        // Start is called before the first frame update
        void Start()
        {
            // 现在外围小人也可以再房间内行走
            seeker.graphMask = GraphMask.FromGraphName("RandomMap");
            seeker.graphMask = GraphMask.FromGraphName("RoomPointMap");
            aiPath.constrainInsideGraph = true;
            aiPath.onReached += OnTargetReached;
            aiPath.onChanged += OnPositionChanged;
            if (rvoController)
            {
                rvoController.collidesWith = RVOLayer.DefaultAgent | RVOLayer.DefaultObstacle;
            }
        }

        private void OnEnable()
        {
            if (!string.IsNullOrEmpty(eventName))
            {
                aiPath.isStopped = false;
                aiPath.updatePosition = true;
                aiPath.updateRotation = true;
                Action();
            }
        }

        private void OnDisable()
        {
            aiPath.isStopped = true;
            aiPath.updatePosition = false;
            aiPath.updateRotation = false;
        }

        private void OnDestroy()
        {
            aiPath.onReached -= OnTargetReached;
            aiPath.onChanged -= OnPositionChanged;
        }
        
        public void SetSeekerLayer()
        {
            
        }

        public void SetToTarget(Vector3 endPos)
        {
            
        }
        public void StartMove(Vector3 endPos, string eventName, int time, int bId,int bIdx, int index, int aId)
        {
            if (!animator && !model)
            {
                animator = transform.GetComponentInChildren<Animator>();
                model = transform.GetComponentInChildren<ModelInstaning>();
            }
            
            outPointIndex = index;
            buildingId = bId;
            bIndex = bIdx;
            areaId = aId;
           
            this.eventName = eventName;
            needTime = time;

            
            rvoController.SetTarget(endPos, 2, 2);
            seeker.StartPath(transform.position, endPos, null);
            if (animator)
                animator.SetTrigger("Walk");
            if(model)
                model.SetTrigger("Walk");
            randomIsWalking = true;
        }

        private void OnTargetReached()
        {
            Action();
        }

        private void OnPositionChanged()
        {
            Action();
        }

        public void ShowEventAction(string eName, int waitTime)
        {
            isShowEvent = true;
            aiPath.isStopped = true;
            //aiPath.updatePosition = false;
            //aiPath.updateRotation = false;
            if (!animator && !model)
            {
                animator = transform.GetComponentInChildren<Animator>();
                model = transform.GetComponentInChildren<ModelInstaning>();
            }

            if (!string.IsNullOrEmpty(eName))
            {
                if (animator)
                    animator.SetTrigger(eName);
                if (model)
                    model.SetTrigger(eName);
            }

            // 等待时间不能过短
            float sec = waitTime / 1000;

            Invoke("EndEventAction", sec);
        }

        public void EndEventAction()
        {
            isShowEvent = false;
            aiPath.isStopped = false;
            aiPath.updatePosition = true;
            aiPath.updateRotation = true;

            if (animator)
                animator.SetTrigger("Walk");
            if (model)
                model.SetTrigger("Walk");
        }

        public void CheckPathNode()
        {
            if (aiPath.hasPath)
            {
                //List<GraphNode> path = aiPath.path.path;
                //for (int i = 0; i < path.Count; i++)
                //{
                //    if (!path[i].Walkable)
                //    {
                //        Debug.Log("有不可行走的点");
                //    }
                //}
            } 
        }

        private void Action()
        {
            
        }
    }
}
